package mystruct;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import vo.person;

import com.opensymphony.xwork2.ActionSupport;

import dao.PersonDao;

public class personaction extends ActionSupport{
	
	public String addform() throws Exception{
		this.LOG.info("personaction.addform");
		this.setRslist(list.shopcar());
		return "list";
	}

	public String add() throws Exception{
		this.LOG.info("personaction.add");
		this.LOG.info("vo.getName():"+vo.getName());
		PersonDao dao = new PersonDao();
		dao.insert(vo);
		HttpSession session=ServletActionContext.getRequest().getSession();
		java.util.ArrayList<person> list =shopcar();
		list.add(vo);
		return "add";
	}
	
	public String list() throws Exception{
		this.LOG.info("personaction.list");
		return "list";
	}
	
	public String updateform() throws Exception{
		this.LOG.info("personaction.updateform");
		
		
		this.setVo(shopcar().get(index));
		return "updateform";
	}
	
	public String update() throws Exception{
		this.LOG.info("personaction.update");
		this.LOG.info("vo.getName():"+vo.getName());
		//PersonDao dao = new PersonDao();
		//dao.insert(vo);
		//HttpSession session=ServletActionContext.getRequest().getSession();
		java.util.ArrayList<person> list =shopcar();
		
		list.set(index, vo);
		//list.add(vo);
		return "update";
	}
	
	
	
	public String delete() throws Exception{
		this.LOG.info("personaction.delete");
		//this.LOG.info("vo.getName():"+vo.getName());
		//PersonDao dao = new PersonDao();
		//dao.insert(vo);
		//HttpSession session=ServletActionContext.getRequest().getSession();
		java.util.ArrayList<person> list =shopcar();
		list.remove(index);
		//list.set(index, vo);
		//list.add(vo);
		return list();
	}
	
	
	
	
	
	
	
	java.util.ArrayList<person> shopcar(){
		//java.util.ArrayList<person> list = new java.util.ArrayList<person>();
		HttpSession session=ServletActionContext.getRequest().getSession();
		java.util.ArrayList<person> list 
		 = (java.util.ArrayList<person>)session.getAttribute("list");
		if(list==null){
			list=new java.util.ArrayList<person>();
			session.setAttribute("list", list);
		}
		return list;
		
	}
	
	
	person vo;
	public void setVo(person vo){
		
		this.vo = vo;
		
	}
	//java.util.ArrayList<person> rslist;
	public java.util.ArrayList<person> getRslist() {
		return shopcar();
	}

	//public void setRslist(java.util.ArrayList<person> rslist) {
	//	this.rslist = rslist;
	//}

	public person getVo() {
		return vo;
	}
	
	int index;
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	
	
}
